package cn.gaotu.system.controller;

import cn.gaotu.basic.util.AjaxResult;
import cn.gaotu.basic.util.PageList;
import cn.gaotu.system.domain.SystemDictionaryType;
import cn.gaotu.system.query.SystemDictionaryTypeQuery;
import cn.gaotu.system.service.SystemDictionaryTypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/systemDictionaryType")
public class SystemDictionaryTypeController {
    @Autowired
    private SystemDictionaryTypeService systemDictionaryTypeService;

    // http://localhost:8080/systemDictionaryType/1 Get
    @ApiOperation(value = "通过ID查询")
    @GetMapping("/{id}")
    public SystemDictionaryType findById(@PathVariable("id")Long id){
        return systemDictionaryTypeService.findById(id);
    }

    // http://localhost:8080/systemDictionaryType Get
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<SystemDictionaryType> findAll(){
        return systemDictionaryTypeService.findAll();
    }

    // http://localhost:8080/systemDictionaryType/1 Delete
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除")
    public AjaxResult remove(@PathVariable("id")Long id){
        try {
            systemDictionaryTypeService.DeleteDep(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }
    // http://localhost:8080/systemDictionaryType Put 参数：{} - 请求体
    @PutMapping
    //@RequestBody SystemDictionaryType systemDictionaryType - 从请求体中获取数据赋值给SystemDictionaryType对象
    //这个是添加或者更新
    @ApiOperation(value = "更新或者保存")
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryType systemDictionaryType) {
        System.out.println(systemDictionaryType);
        try {
            if (systemDictionaryType.getId() == null) {
                systemDictionaryTypeService.save(systemDictionaryType);
            } else {
                systemDictionaryTypeService.update(systemDictionaryType);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }
    // http://localhost:8080/ PosystemDictionaryTypest 参数：{} - 请求体
    //  查询分页
    @PostMapping
    @ApiOperation(value = "查询页面")
    public PageList<SystemDictionaryType> queryPage(@RequestBody SystemDictionaryTypeQuery systemDictionaryTypeQuery) {
        return systemDictionaryTypeService.queryPage(systemDictionaryTypeQuery);
    }
    //批量删除
    @PatchMapping
    @ApiOperation(value = "根据id批量删除")
    public AjaxResult patchRemove(@RequestBody List<Long> ids){
        try {
            systemDictionaryTypeService.patchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "批量删除失败");
        }
    }

}
